<?php
	class dataLayer
	{
		public static $playersData = array();
		public static $accountsData = array();
		
		public static function load()
		{
			foreach( dbLayer::query('SELECT `pid`, `creationdate`, `comment`, `hiddeninfo` FROM `cms_players`;')->fetchAll() as $players )
			{
				self::$playersData[ $players['pid'] ] = array( 'creationdate' => $players['creationdate'], 'comment' => $players['comment'], 'hiddeninfo' => $players['hiddeninfo'] );
			}
			
			foreach( dbLayer::query('SELECT `aid`, `creationdate` FROM `cms_accounts`;')->fetchAll() as $accounts )
			{
				self::$accountsData[ $accounts['aid'] ] = array( 'creationdate' => $accounts['creationdate'] );
			}
			
			return true;
		}
		
		public static function savePlayerData( $ID, $CreationDate, $Comment, $HiddenInfo )
		{
			if( count( dbLayer::query('SELECT `pid`, `creationdate`, `comment`, `hiddeninfo` FROM `cms_players` WHERE `pid`='.intval($ID).';')->fetchAll() === 0 ) )
				dbLayer::query( 'INSERT INTO `cms_players`(`pid`, `creationdate`, `comment`, `hiddeninfo`) VALUES(' . intval($ID) . ', ' . intval($CreationDate) . ', "' . $Comment . '", ' . intval( $HiddenInfo ) . ');' );
			else
				dbLayer::query( 'UPDATE `cms_players` SET `creationdate`=' . intval( $CreationDate ) . ', `comment`="' . $Comment . '", `hiddeninfo`='.intval($HiddenInfo).' WHERE `pid`='.intval( $ID ).';' );
				
			self::$playersData[ $ID ] = array( 'creationdate' => $CreationDate, 'comment' => $Comment, 'hiddeninfo' => $HiddenInfo );	
			return true;
			
		}

		public static function saveAccountData( $ID, $CreationDate )
		{
			if( count( dbLayer::query('SELECT `aid`, `creationdate` FROM `cms_accounts` WHERE `aid`='.intval($ID).';')->fetchAll() === 0 ) )
				dbLayer::query( 'INSERT INTO `cms_accounts`(`aid`, `creationdate`) VALUES(' . intval($ID) . ', ' . intval($CreationDate) . ');' );
			else
				dbLayer::query( 'UPDATE `cms_accounts` SET `creationdate`=' . intval( $CreationDate ) . ' WHERE `aid`='.intval( $ID ).';' );
			
			self::$accountsData[ $ID ] = array( 'creationdate' => $CreationDate );
			return true;

		}

		public static function deleteAccountData( $ID )
		{
			if( count( dbLayer::query('SELECT `aid`, `creationdate` FROM `cms_accounts` WHERE `aid`='.intval($ID).';')->fetchAll() === 0 ) )
				dbLayer::query( 'DELETE FROM `cms_accounts` WHERE `aid`=' . intval($ID) . ';' );
				
			unset( self::$accountsData[ $ID ] );
			return true;
		}
		
		public static function deletePlayerData( $ID )
		{
			if( count( dbLayer::query('SELECT `pid`, `creationdate`, `comment`, `hiddeninfo` FROM `cms_players` WHERE `pid`='.intval($ID).';')->fetchAll() === 0 ) )
				dbLayer::query( 'DELETE FROM `cms_players` WHERE `pid`=' . intval($ID) . ';' );

			unset( self::$playersData[ $ID ] );
			return true;
		}
		
	}
?>